package testrank.affinity;

import java.io.IOException;
import java.util.Collection;
import java.util.Set;
import java.util.SortedMap;
import java.util.Map.Entry;

import testrank.ElementInfo;
import testrank.SourceElementLocator;
import testrank.TestManager;

/**
 * Responsible for scanning all traced files after TestRanking, 
 * and storing WordGroup for all methods.
 *  
 * @author Hagai Cibulski
 */
public class AffinityPreprocessor {

	/**
	 * Enriches all ElementInfo with WordGroup
	 * @throws IOException
	 */
	public void process() throws IOException {
		process(TestManager.locator());
		process(TestManager.testLocator());		
	}
	
	private void process(SourceElementLocator locator) throws IOException {
		
		Set<Entry<String, SortedMap<Integer, ElementInfo>>> allLocations = locator.getAllLocations();
		for (Entry<String, SortedMap<Integer, ElementInfo>> entry : allLocations) {
			String fileName = entry.getKey();
			String filePath = locator.getFilePath(fileName);
			SourceFileAnalyzer analyzer = new SourceFileAnalyzer(filePath); 
			Collection<ElementInfo> codeElements = entry.getValue().values();
			for (ElementInfo elementInfo : codeElements) {
				WordGroup wordGroup = analyzer.analyzeMethod(elementInfo.getBeginLocation(), elementInfo.getEndLocation());
				elementInfo.setWordGroup(wordGroup);
				//elementInfo.enrichWordGroup();
				wordGroup.addDocument(); // seal the analysis of the method
				
				//System.out.println("proceesed: " + elementInfo);
			}
		}
	}
}
